package main.剑指OfferⅠ;

public class Day18 {
    public static void main(String[] args) {
        System.out.println("剑指Offer55-Ⅰ.二叉树的深度");
        System.out.println("剑指Offer55-Ⅱ.平衡二叉树");
    }
}

class Offer55_Ⅰ{
    public int process(TreeNode root){
        if (root==null) return 0;
        int left=maxDepth(root.left)+1;
        int right=maxDepth(root.right)+1;
        return Math.max(left,right);
    }
    public int maxDepth(TreeNode root) {
        return process(root);
    }
}

class Offer55_Ⅱ{
    class Info{
        boolean isBalanced;//当前节点是否为平衡二叉树
        int deep;//当前节点最大深度
        public Info(boolean flag,int num){
            this.isBalanced=flag;
            this.deep=num;
        }
    }
    public Info process(TreeNode root){
        if (root==null) return new Info(true,0);
        Info left=process(root.left);
        Info right=process(root.right);
        int maxDeep=Math.max(left.deep,right.deep)+1;
        boolean flag=left.isBalanced&&right.isBalanced&&(Math.abs(left.deep-right.deep)<=1);
        return new Info(flag,maxDeep);
    }
    public boolean isBalanced(TreeNode root) {
        Info result=process(root);
        return result.isBalanced;
    }
}
